package com.atguigu.test.factory;

import com.atguigu.test.po.FactoryBeanTestBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;

/**
 * @author zp
 * @version 1.0
 * @description: 自定义MyFactoryBean（FactoryBean和BeanFactory的区别）
 * BeanFactory是个Factory，也就是IOC容器或对象⼯⼚，FactoryBean是个Bean。
 * 在Spring中，所有的Bean都是由BeanFactory(也就是IOC容器)来进⾏管理的。
 * 但对FactoryBean⽽⾔，这个Bean不是简单的Bean，⽽是⼀个能⽣产或者修饰对象⽣成的⼯⼚Bean,
 * 它的实现与设计模式中的⼯⼚模式和修饰器模式类似
 * @date 2022/12/1 19:28
 */
@Component
@Slf4j
public class MyFactoryBean implements FactoryBean<FactoryBeanTestBean> {
    @Override
    public FactoryBeanTestBean getObject(){
        log.info("使用MyFactoryBean 初始化");
        FactoryBeanTestBean factoryBeanTestBean = new FactoryBeanTestBean();
        factoryBeanTestBean.setFactory("MyFactoryBean");
        factoryBeanTestBean.setName("factoryBeanTestBean");
        return factoryBeanTestBean;
    }

    @Override
    public Class<FactoryBeanTestBean> getObjectType() {
        log.info("使用MyFactoryBean 获取类型");
        return FactoryBeanTestBean.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}
